package com.ruoyi.web.controller.study_path.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.web.controller.study_path.domain.ARoute;
import com.ruoyi.web.controller.study_path.mapper.ARouteMapper;
import com.ruoyi.web.controller.study_path.service.IARouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 路径管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-04
 */
@Service
public class ARouteServiceImpl implements IARouteService
{
  @Autowired
  private ARouteMapper aRouteMapper;

  /**
   * 查询路径管理
   *
   * @param routeId 路径管理主键
   * @return 路径管理
   */
  @Override
  public ARoute selectARouteByRouteId(Long routeId)
  {
    return aRouteMapper.selectARouteByRouteId(routeId);
  }

  /**
   * 查询路径管理列表
   *
   * @param aRoute 路径管理
   * @return 路径管理
   */
  @Override
  public List<ARoute> selectARouteList(ARoute aRoute)
  {
    return aRouteMapper.selectARouteList(aRoute);
  }

  /**
   * 新增路径管理
   *
   * @param aRoute 路径管理
   * @return 结果
   */
  @Override
  public int insertARoute(ARoute aRoute)
  {
    return aRouteMapper.insertARoute(aRoute);
  }

  /**
   * 修改路径管理
   *
   * @param aRoute 路径管理
   * @return 结果
   */
  @Override
  public int updateARoute(ARoute aRoute)
  {
    return aRouteMapper.updateARoute(aRoute);
  }

  /**
   * 批量删除路径管理
   *
   * @param routeIds 需要删除的路径管理主键
   * @return 结果
   */
  @Override
  public int deleteARouteByRouteIds(Long[] routeIds)
  {
    return aRouteMapper.deleteARouteByRouteIds(routeIds);
  }

  /**
   * 删除路径管理信息
   *
   * @param routeId 路径管理主键
   * @return 结果
   */
  @Override
  public int deleteARouteByRouteId(Long routeId)
  {
    return aRouteMapper.deleteARouteByRouteId(routeId);
  }

  @Override
  public void setRouNode(String newtext2, String class_id, Long userId, String userMessage) {
    String[] result = parseMessageToArray(newtext2);
    String nodes = new String();
    for (int i = 0; i < result.length; i++) {
      System.out.println("result[i] = " + result[i]);
      nodes+= result[i]+"&";
    }
    System.out.println("nodes = " + nodes);
    ARoute aRoute = new ARoute();
    aRoute.setStudentId(userId);
    aRoute.setPath(nodes);
    aRoute.setClassId(Long.valueOf(class_id));
    aRouteMapper.insertARoute(aRoute);

  }

  public static String[] parseMessageToArray(String message) {
    List<String> items = new ArrayList<>();
    Pattern pattern = Pattern.compile("\"([^\"]*)\"");
    Matcher matcher = pattern.matcher(message);

    while (matcher.find()) {
      items.add(matcher.group(1));
    }

    return items.toArray(new String[0]);
  }

}

